<template>
<div class="mainBox">
  <div class="loginBox">
    <div class="loginBoxText">
      <span>文章管理后台系统</span>
    </div>
      <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          class="demo-ruleForm"
          :size="formSize"
          status-icon
      >
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username"  :prefix-icon="UserFilled"/>
        </el-form-item>
        <el-form-item prop="password" style="position: relative">
          <el-input v-model="ruleForm.password"
                    :prefix-icon="Lock"
                    :type="isShowPassword ? '' : 'password'"
                    show-password
          />
        </el-form-item>
        <el-form-item style="position: relative">
          <el-button type="primary" v-onceClick="10000" @click="submitForm(ruleFormRef)">登录</el-button>
        </el-form-item>
      </el-form>
    <div class="toRegisterRemindBox">
      <span><el-link type="info" @click="toRegister" >去注册-></el-link></span>
    </div>
    <div class="loginRemindBox">
      <span>用户名：zcf111</span>
      <span>密码：111111</span>
    </div>
  </div>
</div>
</template>

<script>
import { reactive, ref } from 'vue'
import {useRouter} from "vue-router";
import { ElMessage } from "element-plus";
import { UserFilled, Lock, Hide, View } from '@element-plus/icons-vue'
import {Token} from "@/store";

export default {
  name: "LoginPage",
  setup(){
    let isShowPassword = ref(false)
    const router = useRouter()
    const formSize = ref('default')

    const ruleFormRef = ref()

    const ruleForm = reactive({
      password: '111111',
      username: 'zcf111'
    })

    const rules = reactive({
      password: [
        { required: true, message: '请填写密码', trigger: 'blur' },
        { min: 6, max: 15, message: '6-15位非空格字符', trigger: 'blur' },
      ],
      username: [
        { required: true, message: '请填写用户名', trigger: 'blur' },
        { min: 1, max: 10, message: '1-10位大小写字母和数字', trigger: 'blur' },
      ]
    })


    const store = Token()
    // 方法
    const submitForm = async (formEl) => {
      if (!formEl) return
      await formEl.validate(async(valid) => {
        if (valid) {
          await store.saveToken(ruleForm)
          await router.replace('/')
        } else {
          ElMessage.error('请按要求填写')
        }
      })
    }

    const changePassword = () => {
      isShowPassword.value = !isShowPassword.value
    }

    const toRegister = () => {
      router.replace('/register')
    }


    return {
      formSize,
      ruleFormRef,
      ruleForm,
      rules,
      isShowPassword,
      UserFilled,
      Lock,
      Hide,
      View,
      // 方法
      submitForm,
      changePassword,
      toRegister
    }
  }
}
</script>

<style scoped>
.mainBox{
  width: 100%;
  height: calc(100vh);
  background-color: #2d3a4b;
  position: relative;
}
.loginBox{
  width: 21%;
  height: 280px;
  position: absolute;
  left: 39.5%;
  top: 20%;
}
.loginBoxText{
  width: 100%;
  height: 80px;
  text-align: center;

}
.loginBoxText span{
  line-height: 80px;
  font-size: 25px;
  color: white;
}
.el-form{
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.el-form-item{
  margin: 0 auto 20px;
}
.el-input{
  width: 350px;
}
.icon{
  color: gray;
  font-size: 20px;
  position: absolute;
  right: 40px;
  top: 2px;
  user-select:none;
}
.el-button{
  width: 350px;
  margin: 0 auto;
}
.loginRemindBox{
  width: 100%;
  height: 25px;
  margin-top: 5px;
  line-height: 25px;
  text-align: center;
  color: white;
}
.loginRemindBox span{
  margin-right: 20px;
}
.toRegisterRemindBox{
  width: 100%;
  text-align: center;
  line-height: 30px;
}
.el-link{
  font-size: 15px;
}
</style>
